package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/zigzag-conversion/">6. Z 字形变换</a>
 */
public class Solution6 extends BaseSolution {

    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        StringBuilder sb = new StringBuilder();
        int floor = 0;
        int len = s.length();
        int itemSum = 2 * (numRows - 1);
        while (floor < numRows) {
            int p = floor;
            int step = 2 * (numRows - p - 1);
            while (p < len) {
                sb.append(s.charAt(p));
                if (step == 0) {
                    step = itemSum - step;
                }
                p += step;
                step = itemSum - step;
            }
            floor++;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Solution6 solution = new Solution6();
        System.out.println(solution.convert("PAYPALISHIRING", 4));
    }
}